﻿using pbvs.Calculations;
using pbvs.Models;
using System;
using System.Collections.Generic;
using System.Linq;

namespace pbvs.Data
{
    public class VariablesFull
    {
	    private readonly PbvsContext _db;

	    public VariablesFull(PbvsContext db)
	    {
			_db = db;
	    }

	    public IList<CommutationTable> BuildVariableList(decimal userInterest, decimal userInterestThereafter, decimal? userCola)
        {
            IList<CommutationTable> comTable = new List<CommutationTable>();

                IList<CommutationVariable> vars = (from v in _db.Variables select v).ToList();
                double male2022 = 1000000;
                double female2022 = 1000000;
	            double cola = Convert.ToDouble(userCola ?? 0);
	            double first20Interest = Convert.ToDouble(userInterest);
				double thereafterInterest = Convert.ToDouble(userInterestThereafter);
                decimal malePrevious1994 = vars[0].Male1994UP;
                decimal femalePrevious1994 = vars[0].Female1994UP;
                decimal malePreviousScale = vars[0].MaleAAScale;
                decimal femalePreviousScale = vars[0].FemaleAAScale;
                foreach (CommutationVariable var in vars)
                {
                    if (var.Age > 1)
                    {
                        male2022 = male2022 * (1 - (1 - Convert.ToDouble(var.Male1994UP) / Convert.ToDouble(malePrevious1994)) * Math.Pow((1 - Convert.ToDouble(malePreviousScale)),((DateTime.Now.Year + 10)-1994)));
                        female2022 = female2022 * (1 - (1 - Convert.ToDouble(var.Female1994UP) / Convert.ToDouble(femalePrevious1994)) * Math.Pow((1 - Convert.ToDouble(femalePreviousScale)), ((DateTime.Now.Year + 10) - 1994)));
                    }
                    decimal maleFirst20Dx = Convert.ToDecimal(male2022 * Math.Pow(((1 + cola) / (1 + first20Interest)), var.Age));
                    decimal maleThereafterDx = Convert.ToDecimal(male2022 * Math.Pow(((1 + cola) / (1 + thereafterInterest)), var.Age));
                    decimal femaleFirst20Dx = Convert.ToDecimal(female2022 * Math.Pow(((1 + cola) / (1 + first20Interest)), var.Age));
                    decimal femaleThereafterDx = Convert.ToDecimal(female2022 * Math.Pow(((1 + cola) / (1 + thereafterInterest)), var.Age));
                    malePrevious1994 = var.Male1994UP;
                    femalePrevious1994 = var.Female1994UP;
                    malePreviousScale = var.MaleAAScale;
                    femalePreviousScale = var.FemaleAAScale;
                    CommutationTable com = new CommutationTable
	                                           {
		                                           Age = var.Age,
		                                           Male1994UP = var.Male1994UP,
		                                           MaleAaScale = var.MaleAAScale,
		                                           Female1994UP = var.Female1994UP,
		                                           FemaleAaScale = var.FemaleAAScale,
		                                           Male2022 = Convert.ToDecimal(male2022),
		                                           MaleFirst20Dx = maleFirst20Dx,
		                                           MaleThereafterDx = maleThereafterDx,
		                                           Female2022 = Convert.ToDecimal(female2022),
		                                           FemaleFirst20Dx = Convert.ToDecimal(femaleFirst20Dx),
		                                           FemaleThereafterDx =
			                                           Convert.ToDecimal(femaleThereafterDx)
	                                           };
	                comTable.Add(com);
                }

            return comTable;
        }
    }
}